{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "minute-rwanda",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mxxx\u001b[39m\r\n",
       "defined \u001b[32mclass\u001b[39m \u001b[36mbbb\u001b[39m\r\n",
       "defined \u001b[32mclass\u001b[39m \u001b[36myyy\u001b[39m"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class xxx[+A]\n",
    "class bbb[-A]\n",
    "class yyy[A]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "previous-configuration",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mAnimal\u001b[39m\r\n",
       "defined \u001b[32mclass\u001b[39m \u001b[36mCat\u001b[39m\r\n",
       "defined \u001b[32mclass\u001b[39m \u001b[36mDog\u001b[39m"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "abstract class Animal {\n",
    "    def name:String\n",
    "}\n",
    "\n",
    "case class Cat(name:String) extends Animal\n",
    "case class Dog(name:String) extends Animal\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "overall-helen",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mobject\u001b[39m \u001b[36mCovarianceTest\u001b[39m"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "object CovarianceTest extends App {\n",
    "    def printAnimalNames(animals:List[Animal]):Unit = {\n",
    "        animals.foreach {\n",
    "            animal => println(animal.name)\n",
    "        }\n",
    "    }\n",
    "    val cats:List[Cat] = List(Cat(\"w\"),Cat(\"t\"))\n",
    "    val dogs:List[Dog] = List(Dog(\"f\"),Dog(\"r\"))\n",
    "    \n",
    "    printAnimalNames(cats)\n",
    "    printAnimalNames(dogs)\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "superior-capability",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mPrinter\u001b[39m"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "abstract class Printer[-A] {\n",
    "    def print(value:A) :Unit\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "metric-colony",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mAP\u001b[39m\r\n",
       "defined \u001b[32mclass\u001b[39m \u001b[36mCP\u001b[39m"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class AP extends Printer[Animal] {\n",
    "    def print(animal:Animal):Unit = println(\"111\"+animal.name)\n",
    "}\n",
    "\n",
    "class CP extends Printer[Cat]{\n",
    "    def print(cat:Cat): Unit = println(\"123\"+cat.name)\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "detailed-absence",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123B\n",
      "111B\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\u001b[36mmyCat\u001b[39m: \u001b[32mCat\u001b[39m = \u001b[33mCat\u001b[39m(\u001b[32m\"B\"\u001b[39m)\r\n",
       "defined \u001b[32mfunction\u001b[39m \u001b[36mprintMyCat\u001b[39m\r\n",
       "\u001b[36mcatPrinter\u001b[39m: \u001b[32mPrinter\u001b[39m[\u001b[32mCat\u001b[39m] = ammonite.$sess.cmd13$Helper$CP@2a056f04\r\n",
       "\u001b[36manimalPrinter\u001b[39m: \u001b[32mPrinter\u001b[39m[\u001b[32mAnimal\u001b[39m] = ammonite.$sess.cmd13$Helper$AP@71f5673d"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val myCat: Cat = Cat(\"B\")\n",
    "\n",
    "def printMyCat(printer:Printer[Cat]):Unit = {\n",
    "    printer.print(myCat)\n",
    "}\n",
    "\n",
    "val catPrinter: Printer[Cat] = new CP\n",
    "val animalPrinter: Printer[Animal] = new AP\n",
    "\n",
    "printMyCat(catPrinter)\n",
    "printMyCat(animalPrinter)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "logical-practice",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mContainer\u001b[39m"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Container[A](value:A) {\n",
    "    private var _value:A = value\n",
    "    def getValue:A = _value\n",
    "    def setValue(value: A):Unit = {\n",
    "        _value = value\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "cloudy-pakistan",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "cmd17.sc:2: type mismatch;\n",
      " found   : cmd17.this.cmd16.Container[cmd17.this.cmd10.Cat]\n",
      " required: cmd17.this.cmd16.Container[cmd17.this.cmd10.Animal]\n",
      "Note: cmd17.this.cmd10.Cat <: cmd17.this.cmd10.Animal, but class Container is invariant in type A.\n",
      "You may wish to define A as +A instead. (SLS 4.5)\n",
      "val animalContainer: Container[Animal] = catContainer\n",
      "                                         ^Compilation Failed"
     ]
    },
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "Compilation Failed"
     ]
    }
   ],
   "source": [
    "val catContainer: Container[Cat] = new Container(Cat(\"Felix\"))\n",
    "val animalContainer: Container[Animal] = catContainer\n",
    "animalContainer.setValue(Dog(\"Spot\"))\n",
    "val cat: Cat = catContainer.getValue // "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "smoking-territory",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Scala",
   "language": "scala",
   "name": "scala"
  },
  "language_info": {
   "codemirror_mode": "text/x-scala",
   "file_extension": ".scala",
   "mimetype": "text/x-scala",
   "name": "scala",
   "nbconvert_exporter": "script",
   "version": "2.11.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
